package cn.iocoder.yudao.module.engine.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import cn.iocoder.yudao.module.engine.model.engine.EngineOutputData;
import cn.iocoder.yudao.module.engine.model.context.ControlEngineContext;
import cn.iocoder.yudao.module.engine.model.context.EngineSessionContext;
import cn.iocoder.yudao.module.resource.api.model.AudioDTO;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public class AudioUtils {

    public static AudioDTO randomAudio(EngineSessionContext sessionContext, List<AudioDTO> audioList) {
        if (CollUtil.isEmpty(audioList)) {
            return null;
        }

        if (audioList.size() == 1) {
            return audioList.get(0);
        }

        // audioList排除在usedAudioIdList中的数据
        final Collection<String> usedAudioIds = sessionContext.getUsedAudioIds();
        List<AudioDTO> newAudioList = new ArrayList<>();
        for (AudioDTO audioDto : audioList) {
            if (!usedAudioIds.contains(String.valueOf(audioDto.getAudioId()))) {
                newAudioList.add(audioDto);
            }
        }

        if (newAudioList.size() == 0) {
            log.info("重新建立已使用循环，清空usedAudioIdList中当前的audioList");
            final List<String> audioIdList = audioList.stream().map(v -> String.valueOf(v.getAudioId())).collect(Collectors.toList());
            sessionContext.removeUsedAudioIds(audioIdList);
            newAudioList = audioList;
        }

        if (newAudioList.size() == 1) {
            return newAudioList.get(0);
        }
        final AudioDTO fianlAudioDto = newAudioList.get(RandomUtil.randomInt(0, newAudioList.size() - 1));
        return fianlAudioDto;
    }

    public static List<EngineOutputData.Audio> getAudioList(ControlEngineContext engineContext) {
        final List<EngineOutputData.Audio> audioList = new ArrayList<>();

        final EngineOutputData outputData = engineContext.getOutputData();
        final EngineOutputData.Audio mainAudio = outputData.getMainAudio();
        if (mainAudio != null) {
            audioList.add(mainAudio);
        }

//        final EngineOutputData.Audio backgroundSound = outputData.getAuxiliaryData().getBackgroundSound();
//        if (backgroundSound != null) {
//            audioList.add(backgroundSound);
//        }

        return audioList;
    }

    public static Set<String> getAudioIdsList(ControlEngineContext engineContext) {
        final Set<String> ids = new HashSet<>();

        final EngineOutputData outputData = engineContext.getOutputData();
        final EngineOutputData.Audio mainAudio = outputData.getMainAudio();
        if (mainAudio != null) {
            ids.add(mainAudio.getAudioId());
        }

//        final EngineOutputData.Audio backgroundSound = outputData.getAuxiliaryData().getBackgroundSound();
//        if (backgroundSound != null) {
//            audioList.add(backgroundSound);
//        }
        return ids;
    }

}
